<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>桌游倒计时控制台</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .player-status {
            display: flex;
            justify-content: space-around;
            margin: 20px 0;
        }

        .player-card {
            padding: 15px;
            border-radius: 8px;
            width: 28%;
        }

        .player1 {
            background-color: #ffebee;
            border: 2px solid #e74c3c;
        }

        .player2 {
            background-color: #e3f2fd;
            border: 2px solid #3498db;
        }

        .player3 {
            background-color: #e8f5e9;
            border: 2px solid #2ecc71;
        }

        .current-player {
            border: 4px solid gold;
        }

        .player-name {
            font-weight: bold;
            font-size: 18px;
        }

        .player1 .player-name {
            color: #e74c3c;
        }

        .player2 .player-name {
            color: #3498db;
        }

        .player3 .player-name {
            color: #2ecc71;
        }

        .player-time {
            font-size: 24px;
            margin: 10px 0;
        }

        button {
            padding: 10px 20px;
            margin: 10px 5px;
            font-size: 16px;
            cursor: pointer;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
        }

        button:hover {
            background-color: #2980b9;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>桌游倒计时控制台</h1>

        <div class="player-status">
            <div class="player-card player1" id="player1-card">
                <div class="player-name">玩家1</div>
                <div class="player-time" id="player1-time">03:00</div>
                <div><a href="/player/1" target="_blank">打开玩家1页面</a></div>
            </div>

            <div class="player-card player2" id="player2-card">
                <div class="player-name">玩家2</div>
                <div class="player-time" id="player2-time">03:00</div>
                <div><a href="/player/2" target="_blank">打开玩家2页面</a></div>
            </div>

            <div class="player-card player3" id="player3-card">
                <div class="player-name">玩家3</div>
                <div class="player-time" id="player3-time">03:00</div>
                <div><a href="/player/3" target="_blank">打开玩家3页面</a></div>
            </div>
        </div>

        <div class="controls">
            <button id="reset-btn">重置所有计时器</button>
        </div>
    </div>

    <script>
        const socket = io();

        // 格式化时间为MM:SS
        function formatTime(seconds) {
            const mins = Math.floor(seconds / 60);
            const secs = seconds % 60;
            return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
        }

        // 更新玩家状态显示
        function updateDisplay(data) {
            for (let playerId in data.players) {
                const player = data.players[playerId];
                document.getElementById(`player${playerId}-time`).textContent = formatTime(player.remaining_time);

                // 更新当前玩家高亮
                const card = document.getElementById(`player${playerId}-card`);
                if (parseInt(playerId) === data.current_player) {
                    card.classList.add('current-player');
                } else {
                    card.classList.remove('current-player');
                }
            }
        }

        // 重置按钮事件
        document.getElementById('reset-btn').addEventListener('click', () => {
            socket.emit('reset_all');
        });

        // Socket.IO 事件监听
        socket.on('game_update', (data) => {
            updateDisplay(data);
        });

        socket.on('player_switched', (data) => {
            updateDisplay(data);
        });

        socket.on('game_reset', (data) => {
            updateDisplay(data);
        });

        socket.on('timer_end', (data) => {
            alert(`玩家${data.player}的时间到了！`);
        });
    </script>
</body>

</html>